package org.gy.T11.t7;

import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Server {
    public static void main(String[] args) throws Exception {
        System.out.println("-----服务端启动成功-------");
// 1、创建ServerSocket的对象，同时为服务端注册端⼝。
        ServerSocket serverSocket = new ServerSocket(8080);
// 创建出⼀个线程池，负责处理通信管道的任务。
        ThreadPoolExecutor pool = new ThreadPoolExecutor(16 * 2, 16 * 2, 0,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(8) , Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
        while (true) {
// 2、使⽤serverSocket对象，调⽤⼀个accept⽅法，等待客户端的连接请求
            Socket socket = serverSocket.accept();
            // 3、把这个客户端对应的socket通信管道，交给⼀个独⽴的线程负责处理。
            pool.execute(new ServerReaderRunnable(socket));
        }
    }
}
